/**
 * UI
 * @class 
 */
joCore.UI = {

	/**
	 * syntax sugar of joWindow
	 * @param {Object} param
	 * @return {joWindow}
	 */
	window: function(param){
		return new joWindow(param);	
	},
	
	/**
	 * syntax sugar of joDialog
	 * @return {joDialog}
	 */
	dialog: function(param){
		return new joDialog(param);
	},
	
	/**
	 * yes or no dialog
	 * @return {Deferred|Boolean} 
	 */
	confirm: function(param){
		var deferred = new Deferred();

		var dialog = new joDialog(param);
		dialog.addContent('<button class="button-cancel">Cancel</button>');
		joEvent.addEvent($C('button-cancel', dialog.display.content), 'click', function(){
			deferred.call(false);
			this.close();
		}, false, dialog, true);
		joEvent.addEvent($C('button-ok', dialog.display.content), 'click', function(){ deferred.call(true); }, false, this, true);
	
		return deferred;
	},
	
	/**
	 * enterable dialog
	 * @return {Deferred|String}
	 */
	prompt: function(param){
		var deferred = new Deferred();
		
		var dialog = new joDialog(param);
		dialog.addContent('<input type="text" class="input-prompt" value="' + (param.defaultStr || '') + '" />');
		joEvent.addEvent($C('button-ok', dialog.display.content), 'mousedown', function(){
			var inputStr = $C('input-prompt', this.display.content).value;
			deferred.call(inputStr);
			this.close();
		}, false, dialog, true);
		
		return deferred;
	},

	/**
	 * syntax sugar of joTab
	 * @param {HTMLULElement} ul
	 */
	tab: function(ul){
		return new joTab(ul);
	},
	
	
	/**
	 * Tree
	 * @param 
	 */
	tree: function(table){},

};


/**
 * タブ
 * @constructor
 * @param {HTMLULElement} ul
 */
function joTab(ul){
	this.init(ul);
}
joTab.prototype = {
	
	/**
	 * 初期化処理
	 * @private
	 */
	init: function(ul){
		joCore.Node.addClass(ul, 'tabs');
		this.tabs = ul;
		this.tablist = uuQuery.tag('li', ul);
		this.tabpanels = [];
		this.tablist.forEach(function(tab, index){
			joCore.Node.addClass(tab, 'tab');
			joEvent.addEvent(tab, 'click', this._onClickTab, false, this);
			
			var tabpanel = this.getTabpanel(tab);
			joCore.Node.addClass(tabpanel, 'tabpanel');
			this.tabpanels[index] = tabpanel;
		}, this);
		
		//最初のタブを選択する
		this.changeTab(this.tablist[0]);	
	},	
	
	/**
	 * tabpanelを得る
	 * @param {Tab} tab
	 */
	getTabpanel: function(tab){
		return uuQuery.id(tab.getAttribute('tabpanel'));
	},

	/**
	 * タブをクリックしたときに呼び出される
	 * @param {Event} event
	 */
	_onClickTab: function(event){
		this.changeTab(event.target);
	},

	/**
	 * タブを変更する
	 * @param {Tab} tab
	 */
	changeTab: function(tab){
		var tabpanel = this.getTabpanel(tab);
		var oldSelected = this.tabpanels.filter(function(tabpanel){ return tabpanel.hasAttribute('selected'); })[0];
	
		if(oldSelected) oldSelected.removeAttribute('selected');
		tabpanel.setAttribute('selected', 'true');
	}

};